/**
 * Created by huangzy on 2015/5/22.
 * //wx.12301.cc/tpl/membercard/js/membercard.renzheng.js
 */

var XlsUploader = ZFT.Util.Class({
    statics: {
        ltie9: false
    },
    EVENTS: {
        "change input.fileInp": "onFileInpChange"
    },
    init: function (opt) {
        var that = this;
        var container = this.container = opt.container;
        var containerId = container.attr("id");
        var form = this.form = container.find("form");
        var fileInp = this.fileInp = container.find("input[type=file]");
        var loading = this.loading = container.find(".loading");
        var fileTitle = this.fileTitle = container.find(".fileTitle");
        var xlsUrlInp=this.xlsUrlInp=container.find(".xlsUrlInp");
        //var stepBox4=this.stepBox4=$(".stepBox4");
        that.fileInp[0].onchange = function (e) {
            that.onFileInpChange(e);
        }
        that.fileInp[0].onclick=function(){
            that.onFileInpClick();
        }
    },
    onFileInpClick:function(e){
        var that=this;
        //that.stepBox4.html("");
        that.fileInp.val("");
        that.xlsUrlInp.val("").attr("data-name", "");
        that.fileTitle.text("");
    },
    onFileInpChange: function (e) {
        var that = this;
        var tarInp = $(e.currentTarget);
        var file = tarInp.get(0).files[0];
        //$("#stepBox4").html("");
        that.xlsUrlInp.val("").attr("data-name", "");
        if (tarInp.siblings("#checkDataBtn")) {
            $(".m-btn.m-btn_no_border").removeAttr("disabled").css("display","inline-block");
        }
        if (!file) {
            return that.fileTitle.text("");
        }
        /*if (file.type != "application/vnd.ms-excel") {
            that.fileTitle.text("");
            tarInp.val("");
            return ZFT.Util.STip("fail", "请选择正确的数据模板文件！", 3000);
        }*/
        that.onload();
        that.form.submit();
    },
    oncomplete: function (res) {
        var that = this;
        if (res.status == "ok" && res.key) {
            that.onsuccess(res);
        } else if (res.status === "fail") {
            that.onfail(res);
        }
        that.loading.css("display", "none");
    },
    onload: function () {
        var that = this;
        that.loading.css("display", "inline-block");
    },
    onsuccess: function (res) {
        var that = this;
        that.xlsUrlInp.val(res.key).attr("data-name", res.filename);
        that.fileTitle.text(res.filename);
        ZFT.Util.STip("success", '文件上传成功!');

    },
    onfail: function (res) {
        return ZFT.Util.STip("fail", res.key);
    },


})


module.exports = XlsUploader;








